<?php


namespace App\Services;

use Swoole\WebSocket\Frame;
use Swoole\WebSocket\Server;

/**
 * 消息处理
 * Class MessageService
 * @package App\Services
 */
class MessageService
{
    public static function switch(Frame $frame, Server $server)
    {
        $message = json_decode($frame->data, true);

        if (is_null($message)) {
            $server->push($frame->fd, error('message content error'));
            return;
        }

        if (!array_key_exists('action', $message)) {
            $server->push($frame->fd, error('message action error'));
            return;
        }

        match ($message['action']) {
            'send_msg_to_user' => ChatService::send($frame, $server, $message),
            'send_smg_to_group' => GroupService::send($frame, $server, $message),
            default => ''
        };
    }

}